package tr.edu.metu.is.mega.server.business;

import java.util.List;

import tr.edu.metu.is.mega.common.dto.AnnouncementDTO;
import tr.edu.metu.is.mega.common.dto.CourseDTO;
import tr.edu.metu.is.mega.server.dao.AnnouncementDAO;
import tr.edu.metu.is.mega.server.entity.Announcement;
import tr.edu.metu.is.mega.server.entity.Course;

/**
 * Class to manage announcements.
 */
public class AnnouncementManager {

    private final AnnouncementDAO dao;

    private final AnnouncementDTOHelper helper;

    /**
     * Default constructor.
     */
    public AnnouncementManager() {
        dao = new AnnouncementDAO();
        helper = new AnnouncementDTOHelper();
    }

    /**
     * Creates an announcement.
     * 
     * @param announcement announcement
     * @return announcement
     */
    public AnnouncementDTO createAnnouncement(AnnouncementDTO announcement) {
        Announcement entity = new Announcement();
        helper.convertToEntity(announcement, entity);
        return helper.convertToDTO(dao.create(entity));
    }

    /**
     * Updates a announcement.
     * 
     * @param announcement announcement
     * @return announcement
     */
    public AnnouncementDTO updateAnnouncement(AnnouncementDTO announcement) {
        Announcement entity = new Announcement();
        helper.convertToEntity(announcement, entity);
        return helper.convertToDTO(dao.update(entity));
    }

    /**
     * Deletes a announcement.
     * 
     * @param announcement announcement
     */
    public void deleteAnnouncement(AnnouncementDTO announcement) {
        dao.deleteById(announcement.getPkid());
    }

    /**
     * Returns list of announcements in a course.
     * 
     * @param course course
     * @return list of announcements
     */
    public List<AnnouncementDTO> getAnnouncements(CourseDTO course) {
        Announcement announcement = new Announcement();

        Course courseEntity = new Course();
        courseEntity.setPkid(course.getPkid());
        announcement.setCourse(courseEntity);

        List<Announcement> announcementList = dao.findByExample(announcement, false);

        return helper.toDTO(announcementList);
    }

}
